package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.web.entity.WebtEntity;
import com.luoye.lj.wstool.web.entity.WebtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;
import com.luoye.lj.wstool.wsdm.entity.WsdtFieldDesc;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/7/7
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class HtmlFieldsCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public HtmlFieldsCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if(wsdtField.getWsdtFieldDesc()==null) {
                        if (wsdtField.getFieldType().equals("Date")) {
                            code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: true, text: \"" + wsdtField.getDescription() + "\", inTable: true,render:function(row){\n" +
                                    "                       return (new Date(row." + wsdtField.getFieldName() + ")).Format(\"yyyy-MM-dd hh:mm\");\n" +
                                    "                    }},\n";
                        } else {
                            code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: true, text: \"" + wsdtField.getDescription() + "\", inTable: true},\n";
                        }
                    }else {
                        String autoHide="false";
                        if(wsdtField.getWsdtFieldDesc().isAutoHide()){
                            autoHide="true";
                        }
                        String inTable="false";
                        if(wsdtField.getWsdtFieldDesc().isInTable()){
                            inTable="true";
                        }
                        if(wsdtField.getWsdtFieldDesc().getEnumItems()!=null){
                            code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: "+autoHide+", text: \"" + wsdtField.getWsdtFieldDesc().getText() + "\", inTable: "+inTable+",render:function(row){\n" +
                                    "                    switch (row."+ wsdtField.getFieldName() +") {\n";
                            for(WsdtFieldDesc.EnumItem enumItem:wsdtField.getWsdtFieldDesc().getEnumItems()){
                                code+="                                case "+(wsdtField.getFieldType().equals("String")?"'"+enumItem.getValue()+"'":enumItem.getValue())+":\n" +
                                        "                                    return \""+enumItem.getText()+"\";\n";
                            }
                            code += "                            }\n" +
                                    "                            return \"未知\";\n" +
                                    "                    }},\n";
                        }else if(wsdtField.getWsdtFieldDesc().getRelatedEntity()!=null){
                            code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: "+autoHide+", text: \"" + wsdtField.getWsdtFieldDesc().getText() + "\", inTable: "+inTable+",render:function(row){\n";
                            String entityName=wsdtField.getWsdtFieldDesc().getRelatedEntity().getEntityName();
                            String en= StringUtils.makeFirstCharLowercase(entityName);
                            code +="                        return "+wsdtField.getFieldName()+entityName+"Map[row."+wsdtField.getFieldName()+"];\n";
                            code += "                    }},\n";
                        }else{
                            if (wsdtField.getFieldType().equals("Date")) {
                                code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: "+autoHide+", text: \"" + wsdtField.getWsdtFieldDesc().getText()  + "\", inTable: "+inTable+",render:function(row){\n" +
                                        "                       return (new Date(row." + wsdtField.getFieldName() + ")).Format(\"yyyy-MM-dd hh:mm\");\n" +
                                        "                    }},\n";
                            } else {
                                code += "                    {name: \"" + wsdtField.getFieldName() + "\", autoHidden: "+autoHide+", text: \"" + wsdtField.getWsdtFieldDesc().getText()  + "\", inTable: "+inTable+"},\n";
                            }}
                    }
                }
            }
        }
        if(!"".equals(code)){
            code=code.substring(0,code.length()-2);
        }
        return code;
    }
}
